package com.bleem.ds.xinshou.a02;

/**
 * @author SuYanchun
 * @since 2024-08-08 下午4:36
 */
public class ProbabilityDemo1 {

    //已知f1() 按照固定概率返回0和1，但是0和1概率不同，求g()等概率返回0和1

    public static void main(String[] args) {
        int testCount = 100000;
        int count = 0;
        for (int i = 0; i < testCount; i++) {
            if (g() == 1) {
                count++;
            }
        }
        System.out.println(Double.parseDouble(String.valueOf(count))/Double.parseDouble(String.valueOf(testCount)));
    }

    public static int f1(){
        return Math.random() < 0.84 ? 0 : 1;
    }

    public static int g(){
        int a;
        int b;
        do {
            a = f1();
            b = f1();
        } while (a == b);

        return a < b ? 0 : 1;

    }
}
